//
//  main.m
//  glfwdemo
//
//  Created by 王洪飞 on 2024/2/22.
//

#import <Foundation/Foundation.h>
#include <glad/glad.h>
#import "glfw3.h"


// Standard Headers
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string>
#include <iostream>
#include <glm/glm.hpp>
#include "shader_s.hpp"

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

void processInput(GLFWwindow *window);

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        
        glfwInit();
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        
        
#ifdef __APPLE__
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
        
        
        GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "FirstGL", NULL, NULL);
        if (window == NULL)
        {
           printf("open window failed \n");
           glfwTerminate();
           return -1;
        }
        
        printf("open window success \n");
        glfwMakeContextCurrent(window);
        glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
        
        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
            std::cout << "failed init glad" << std::endl;
            return -1;
        }
        
        Shader ourShader("/Users/wanghongfei/Documents/opengles-demo/glfwdemo/glfwdemo/3.3.shader.vs", "/Users/wanghongfei/Documents/opengles-demo/glfwdemo/glfwdemo/3.3.shader.fs"); // you can name your shader files however you like
        
        
        float vertices[] = {
            0.5f,  0.5f, 0.0f,  // top right
                     0.5f, -0.5f, 0.0f,  // bottom right
                    -0.5f, -0.5f, 0.0f,  // bottom left
                    -0.5f,  0.5f, 0.0f   // top left
        };
        
        unsigned int indices[] = {  // note that we start from 0!
              0, 1, 3,  // first Triangle
              1, 2, 3   // second Triangle
          };
        
        
        GLuint vbo, vao, ebo;
        glGenVertexArrays(1,&vao);
        glGenBuffers(1,&vbo);
        glGenBuffers(1, &ebo);
        
        glBindVertexArray(vao);
        
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER,  sizeof(vertices), vertices, GL_STATIC_DRAW);
        
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

        
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);
        
        
        glBindVertexArray(0);
        
        
        float vertices1[] = {
            -0.8f, -0.8f, 0.0f,
             0.5f, -0.8f, 0.0f,
             0.0f,  0.8f, 0.0f
        };
        
        
        
        GLuint vbo1, vao1;
        glGenVertexArrays(1,&vao1);
        glGenBuffers(1,&vbo1);
        
        glBindVertexArray(vao1);
        
        glBindBuffer(GL_ARRAY_BUFFER, vbo1);
        glBufferData(GL_ARRAY_BUFFER,  sizeof(vertices1), vertices1, GL_STATIC_DRAW);
        
        
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);
        
        
        glBindVertexArray(0);
        
        
        while (!glfwWindowShouldClose(window)) {
            
            processInput(window);
            
            glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT);
            glBindVertexArray(vao);
            
//            glUseProgram(shaderProgram);
            ourShader.use();
//            glDrawArrays(GL_TRIANGLES, 0, 3);
            glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS ? glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0) : glDrawElements(GL_LINE_LOOP, 6, GL_UNSIGNED_INT, 0);
            
            glfwSwapBuffers(window);
            glfwPollEvents();
        }

        glfwTerminate();
        printf("return \n");
    }
    return 0;
}

void processInput(GLFWwindow *window)
{

    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS){
        glfwSetWindowShouldClose(window, true);
    }

}
